<?php

namespace mpend\modules\share\models;

use Yii;
use common\models\user\User;
use common\models\workbook\WorkbookMember;
use common\models\workbook\WorkbookInvite;
use yii\helpers\ArrayHelper;

/**
 * 班组记工本加入模型
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class BookShareForm extends \common\base\Model {

    /**
     * @var string 
     */
    public $user_id;

    /**
     * @var string 班组长/工头姓名
     */
    public $workbook_id;

    /**
     * @var string 记工本名称
     */
    public $from_user_id;

    /**
     * @var string 记工本名称
     */
    public $member_id;

    /**
     * @var string 记工薄类型
     */
    public $type;

    /**
     * @var ShareBook 记工薄
     */
    private $_workbook;

    /**
     * @var WorkbookMember 记工薄
     */
    private $_member;

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['workbook_id', 'from_user_id', 'member_id', 'user_id'], 'integer'],
            [['user_id', 'workbook_id', 'type'], 'required'],
            [['type'], 'in', 'range' => WorkbookInvite::getConstants('type'), 'message' => '{attribute}不在允许范围内'],
            [['workbook_id'], 'validateWorkbook'],
            [['from_user_id'], 'validateFromUser'],
            [['member_id'], 'validateMember', 'skipOnEmpty' => false],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'title' => '记工本名称',
            'headman' => '班组长/工头姓名',
            'teamwork' => '是否允许工人记工',
        ];
    }

    public function validateWorkbook($attribute) {
        if ($this->hasErrors()) {
            return;
        }
        $model = ShareBook::findOne($this->$attribute);
        if (!$model) {
            return $this->addError($attribute, '记工薄信息不存在');
        }
        $this->_workbook = $model;
    }

    public function validateFromUser($attribute) {
        if ($this->hasErrors()) {
            return;
        }
        $user = User::findOne($this->$attribute);
        if (!$user) {
            return $this->addError($attribute, '记工薄分享已失效');
        }
    }

    public function validateMember($attribute) {
        if ($this->hasErrors()) {
            return;
        }
        if ($this->$attribute) {
            $member = WorkbookMember::findOne($this->$attribute);
            if (!$member) {
                return $this->addError($attribute, '记工薄成员信息不存在');
            }
            if ($member->user_id != $this->user_id) {
                return $this->addError($attribute, '无效分享连接');
            }
            $this->_member = $member;
        } else {
            $this->_member = WorkbookMember::find()->active(WorkbookMember::STATUS_ACTIVE)->andWhere([
                'workbook_id' => $this->workbook_id,
                'user_id' => $this->user_id,
            ])->one();
        }
    }

    public function getWorkbook() {
        return $this->_workbook;
    }

    public function getMember() {
        return $this->_member;
    }

    public function getContent() {
        if ($this->type == WorkbookInvite::TYPE_JOIN) {
            return '我创建了一个班组记工本，邀请你加入，实时记工、快捷对账';
        } else if ($this->type == WorkbookInvite::TYPE_MANAGE) {
            return '我创建了一个班组记工本，邀请你共同管理共享记工本，实时记工、快捷对账';
        } else {
            return '我创建了一个班组记工本，邀请绑定你的账号，实时记工、快捷对账';
        }
    }

    public function save() {
        if (!$this->validate()) {
            return $this->dumpError();
        }
        if ($this->from_user_id == $this->user_id) {
            return [
                'qqq'
            ];
        }
        $model = WorkbookInvite::findOrNew([
            'workbook_id' => $this->workbook_id,
            'user_id' => $this->user_id,
        ]);
        if ($model->isNewRecord) {
            $model->setAttributes([
                'from_user_id' => $this->from_user_id,
                'type' => $this->type,
                'description' => $this->getContent(),
                'member_id' => (int) $this->member_id,
                'joined_at' => 0,
                'expired_at' => time() + 86400,
                'status' => WorkbookInvite::STATUS_DEFAULT,
            ]);
            if (!$model->save()) {
                return $model->dumpError();
            }
        }
        return [
            'invite' => $model,
            'book' => $this->getWorkbook(),
            'member' => $this->getMember(),
        ];
    }

}
